package saucaca.Adapter;

import java.util.List;

import saucaca.Object.NewsObject;
import saucaca.Object.SlideObject;
import saucaca.Util.AppController;
import saucaca.fragment.ArtistListfragment;
import saucaca.fragment.NewDetailFragment;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lemon.kenhnhacvn.BaseActivity;
import com.lemon.kenhnhacvn.MainActivity;
import com.lemon.kenhnhacvn.R;

public class NewsAdapter extends BaseAdapter {
	Context context;
	private String tag;
	private LayoutInflater inflater;
	private List<NewsObject> list;
	ArtistListfragment listArtistfragment;
	ImageLoader imageLoader;
	public final static String keyBundle = "$";

	public NewsAdapter(Context context, List<NewsObject> _list, String tag) {
		list = _list;
		this.tag = tag;
		this.context = context;
		this.inflater = LayoutInflater.from(context);
	}

	public void update(List<NewsObject> list) {
		this.list = list;
		notifyDataSetChanged();
	}

	class ViewHolder {
		LinearLayout layout;
		ImageView imgeAvatar;
		TextView textViewContent;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_news, null);
			holder.layout = (LinearLayout) convertView.findViewById(R.id.new_layout);
			holder.imgeAvatar = (ImageView) convertView.findViewById(R.id.new_image);
			holder.textViewContent = (TextView) convertView.findViewById(R.id.new_text_content);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.layout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				NewsObject newsObject = list.get(position);

				if (tag.equals(NewDetailFragment.class.getName())) {
					((MainActivity) context).reloadFragment(newsObject);
				} else {
					((MainActivity) context).switchContent(NewDetailFragment.class.getName(), newsObject);
				}
			}
		});
		holder.textViewContent.setText(list.get(position).getTXNAME());
//		imageLoader.displayImage(list.get(position).getAVATAR(), holder.imgeAvatar);
		try {
			imageLoader=((BaseActivity)context).imageLoader;
		} catch (Exception e) {
			imageLoader=AppController.getInstance().getImageLoader();
		}
		
		imageLoader.get(list.get(position).getAVATAR(), ImageLoader.getImageListener( holder.imgeAvatar,
	         R.drawable.ic_default, R.drawable.ic_default));
		return convertView;
	}
}
